#!/bin/sh

# $Id$

# /etc/rc - master bootup script, invokes php setup
# part of pfSense by Scott Ullrich
# Copyright (C) 2004 Scott Ullrich, All rights reserved.
# originally based on m0n0wall (http://neon1.net/m0n0wall)
# Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
# All rights reserved.

#/bin/stty status '^T'
#/bin/stty susp '^-' intr '^-' quit '^-'

#trap : 2
#trap : 3

HOME=/
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
export HOME PATH

# Set our operating platform
PLATFORM=`cat /etc/platform`

# Set our current version
version=`cat /etc/version`

if [ "$PLATFORM" = "cdrom" ]; then
	/etc/rc.cdrom
fi

if [ "$PLATFORM" = "embedded" ]; then
	/etc/rc.embedded
fi

if [ "$PLATFORM" = "stairway" ]; then
#	mdmfs -S -M -s 4m md /var/run
fi

product=`cat /usr/local/stairway/inc/globals.inc | grep product_name | cut -d'"' -f4`
hideplatform=`cat /usr/local/stairway/inc/globals.inc | grep hideplatform | wc -l`

if [ "$hideplatform" -gt "0" ]; then
	platformbanner="" # hide the platform
else
	platformbanner=" on the '${PLATFORM}' platform"
fi

echo
cat /usr/local/stairway/data/stairway-logo-small.txt
echo
echo
echo "Welcome to ${product} ${version} ${platformbanner} ..."
echo

# Enable console output if its muted.
/sbin/conscontrol mute off >/dev/null

# Mount memory file system if it exists
echo -n "Mounting filesystems..."
/sbin/mount -a

# Mount /. If it fails run a fsck.
if [ ! "$PLATFORM" = "cdrom" ] ; then
	/sbin/mount -uw / || (/sbin/fsck -fy; /sbin/mount -uw /)
fi

if [ "$PLATFORM" = "cdrom" ] ; then
	# do nothing for cdrom platform
elif [ "$PLATFORM" = "embedded" ] ; then
    # do nothing for embedded platform
else
    SWAPDEVICE=`cat /etc/fstab | grep swap | cut -f1`
    /sbin/swapon -a 2>/dev/null >/dev/null
fi
echo " done."


echo -n "Creating symlinks..."

# Repair symlinks if they are broken
if [ ! -L /etc/syslog.conf ]; then
	rm -rf /etc/syslog.conf
	ln -s /var/etc/syslog.conf /etc/syslog.conf
fi

# Repair symlinks if they are broken
if [ ! -L /etc/hosts ]; then
	rm -rf /etc/hosts
	ln -s /var/etc/hosts /etc/hosts
fi

if [ ! -L /etc/resolv.conf ]; then
    rm -rf /etc/resolv.conf
    ln -s /var/etc/resolv.conf /etc/resolv.conf
fi

# Make sure our /tmp is 777 + Sticky
chmod 1777 /tmp

if [ ! -L /etc/dhclient.conf ]; then
    rm -rf /etc/dhclient.conf
fi

if [ ! -L /etc/sasyncd.conf ]; then
    mkdir -p /var/etc/
    touch /var/etc/sasyncd.conf
    rm -rf /etc/sasyncd.conf
    ln -s /var/etc/sasyncd.conf /etc/sasyncd.conf
    chown root:wheel /var/etc/sasyncd.conf
    chmod 0600 /var/etc/sasyncd.conf
fi

if [ ! -d /var/tmp ]; then
	mkdir -p /var/tmp
fi

if [ ! -d /usr/local/stairway/conf/backup/ ]; then
	mkdir -p /usr/local/stairway/conf/backup/ 
fi

if [ ! -f /var/db/ez-ipupdate.cache ]; then
	touch /var/db/ez-ipupdate.cache
fi

# OpenVPN storage
if [ ! -d /var/etc/openvpn ]; then
	mkdir -p /var/etc/openvpn
fi
if [ ! -d /var/etc/openvpn-csc ]; then
	mkdir -p /var/etc/openvpn-csc
fi

set -T
trap "echo 'Reboot interrupted'; exit 1" 3

# Remove old nameserver resolution files
rm -f /var/etc/nameserver*

# Create uploadbar tmp directory
mkdir -p /tmp/uploadbar
chmod 0777 /tmp/uploadbar

# make some directories in /var
mkdir -p /var/run /var/log /var/etc /var/db/entropy /var/at/jobs/ /var/empty 2>/dev/null
rm /var/log/* 2>/dev/null
rm -rf /var/run/*

echo -n "."
DISABLESYSLOGCLOG=`cat /usr/local/stairway/conf/config.xml | grep disablesyslogclog | wc -l | awk '{ print $1 }'`
if [ "$DISABLESYSLOGCLOG" -gt "0" ]; then 
	touch /var/log/system.log
	touch /var/log/filter.log
	touch /var/log/dhcpd.log
	touch /var/log/vpn.log
	touch /var/log/openvpn.log
	touch /var/log/portalauth.log
	touch /var/log/ipsec.log
	touch /var/log/relayd.log
	touch /var/log/lighttpd.log
	touch /var/log/ntpd.log
else 
	ENABLEFIFOLOG=`cat /usr/local/stairway/conf/config.xml | grep usefifolog | wc -l | awk '{ print $1 }'`
	if [ "$ENABLEFIFOLOG" -gt "0" ]; then
		# generate fifolog files
		/usr/sbin/fifolog_create -s 511488 /var/log/system.log
		/usr/sbin/fifolog_create -s 511488 /var/log/filter.log
		/usr/sbin/fifolog_create -s 50688 /var/log/dhcpd.log
		/usr/sbin/fifolog_create -s 50688 /var/log/vpn.log
		/usr/sbin/fifolog_create -s 50688 /var/log/openvpn.log
		/usr/sbin/fifolog_create -s 50688 /var/log/portalauth.log
		/usr/sbin/fifolog_create -s 50688 /var/log/ipsec.log
		/usr/sbin/fifolog_create -s 50688 /var/log/relayd.log
		/usr/sbin/fifolog_create -s 50688 /var/log/lighttpd.log
		/usr/sbin/fifolog_create -s 50688 /var/log/ntpd.log
	else 
        touch /var/log/system.log
        touch /var/log/filter.log
        touch /var/log/dhcpd.log
        touch /var/log/vpn.log
        touch /var/log/openvpn.log
        touch /var/log/portalauth.log
        touch /var/log/ipsec.log
        touch /var/log/relayd.log
        touch /var/log/lighttpd.log
        touch /var/log/ntpd.log		
	fi
fi 
# change permissions on newly created fifolog files.
chmod 0600 /var/log/*.log

echo -n "."
DEVFS=`mount | grep devfs | wc -l | cut -d" " -f8`
if [ "$DEVFS" = "0" ]; then
    mount_devfs devfs /dev
fi

# Create an initial utmp file
cd /var/run && cp /dev/null utmp && chmod 644 utmp

echo -n "."
/sbin/ldconfig -elf /usr/lib /usr/local/lib /lib

# Make sure /etc/rc.conf doesn't exist.
if [ -f /etc/rc.conf ]; then
    rm -rf /etc/rc.conf
fi

# Launching kbdmux(4)
if [ -f "/dev/kbdmux0" ]; then
	echo -n "."
	kbdcontrol -k /dev/kbdmux0 < /dev/console
	[ -c "/dev/atkbd0" ] && kbdcontrol -a atkbd0 < /dev/console
	[ -c "/dev/ukbd0" ] && kbdcontrol -a ukbd0 < /dev/console
fi

# Fire up unionfs if mount points exist.
if [ -f /dist/uniondirs ]; then
	echo -n "."
	/etc/rc.d/unionfs start
fi

echo "done."

# Recreate capabilities DB
cap_mkdb /etc/login.conf

# Run the php.ini setup file and populate
# /usr/local/etc/php.ini and /usr/local/lib/php.ini
/etc/rc.php_ini_setup

# let the PHP-based configuration subsystem set up the system now
echo -n "Launching PHP init system..."
rm -f /usr/local/stairway/conf/backup/backup.cache
rm -f /root/lighttpd*
/etc/rc.bootup

echo -n "Starting CRON... "
cd /tmp && /usr/sbin/cron -s 2>/dev/null
echo "done."


/usr/local/bin/php -f /etc/rc.conf_mount_ro

# Remove stale files that have already been processed by bootup
# scripts
rm -f /tmp/filter_dirty
rm -f /tmp/rc.linkup
nohup /usr/bin/nice -n20 /usr/local/stairway/tools/check_reload_status 2>/dev/null &

chmod a+rw /tmp/.

echo "Bootup complete"

/usr/local/stairway/tools/beep.sh start 2>&1 >/dev/null

exit 0
